/**
 * 
 */
package backend;

import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 * @author Christian
  * @since 0.14
 */
public class FileChooser {
	
	/**
	 * 
	 */
	private Component panel = null;
	
	/**
	 * 
	 * @param panel Panel das überlagert werden soll
	 */
	public FileChooser(Component panel) {

		this.panel = panel;
		
	}
	
	/**
	 * 
	 * @param mode ChooseMode 1 open 0 del
	 * @return Das gewählte File Object
	 */
	public File chooseFile(int mode) {

		JFileChooser chooser = new JFileChooser();
		File saveFile = new File("zauberInfo.xml");
		chooser.setSelectedFile(saveFile);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.addChoosableFileFilter(new FileFilter() {
			
			@Override
			public boolean accept(File f) {

				if (f.isDirectory()) {
					return true;
				}
				return f.getName().toLowerCase().endsWith(".xml");
			}
			
			public String getDescription() {

				return "xmls";
			}
		});
		chooser.setMultiSelectionEnabled(false);
		if (mode == 1) {
			int erg = chooser.showOpenDialog(panel);
			if (erg == JFileChooser.CANCEL_OPTION || erg == JFileChooser.ERROR_OPTION) {
				return null;
			}
		} else {
			int erg = chooser.showSaveDialog(panel);
			if (erg == JFileChooser.CANCEL_OPTION || erg == JFileChooser.ERROR_OPTION) {
				return null;
			}
		}
		saveFile = chooser.getSelectedFile();
		return saveFile;
	}
}
